//
//  SSOauthViewController.swift
//  weibo
//
//  Created by 邵帅 on 16/6/24.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

class SSOauthViewController: UIViewController {
    
    var webView : UIWebView = UIWebView()
    
    override func loadView() {
        
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 拼接地址URL + 必选参数
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=4270406730&redirect_uri=http://www.baidu.com"
        
        let url = NSURL(string: urlString)
        
        webView.loadRequest(NSURLRequest(URL: url!))
        
        // 设置导航栏
        setupNav()
        
        // webView代理
        webView.delegate = self
    }

}

// 导航栏
extension SSOauthViewController {
    
    private func setupNav() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .Plain, target: self, action: #selector(SSOauthViewController.cancelClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: #selector(SSOauthViewController.addClick))
    }
    
    // 返回
    @objc private func cancelClick() {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // 自动填充
    @objc private func addClick() {
        
        let js = "document.getElementById('userId').value = 'itcast123456@hotmail.com';document.getElementById('passwd').value = 'itcast123456'"
        
        webView.stringByEvaluatingJavaScriptFromString(js)
        
    }
   
}


// webview代理方法
extension SSOauthViewController : UIWebViewDelegate {
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        if let urlString = request.URL?.absoluteString {
            
            if urlString.hasPrefix(AppRedirectURL) {
                
                printLog(urlString)
                //http://www.baidu.com/?code=2a4929c614f0ec40a2685918b7d1039a
                
                // 截取code  
                if let quary = request.URL?.query {
                    
                    
                    let startIndex = "code=".endIndex
                    
                    let code = quary.substringFromIndex(startIndex)
                    
                    printLog(code)
                    
                    let webSuccess = {
                        
                        printLog("请求成功")
                        
                        self.dismissViewControllerAnimated(true, completion: nil)
                        
                        // 成功跳转界面
                        NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChanged, object: self)
                    }
                    
                    let webFalsed = {
                        
                        printLog("请求失败")
                    }
                    
                    SSOauthModel.shareInstance.loadToken(code, success: webSuccess, falsed: webFalsed)
                    
                    
                    return false
                }
            }
            
        }
        
        return true
    }
  
}




